package com.lwhtarena.netty.netty4.downClient;

import org.gdal.gdal.gdal;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.ogr;

public class DXFtoGeoJSONConverter {

    public static void main(String[] args) {
        // Set the path to your DXF file and GeoJSON output file
        String inputDxfPath = "F:\\workes\\test-items\\netty-file\\netty-client\\doc\\map-of-uae.dxf";
        String outputGeoJsonPath = "F:\\workes\\test-items\\netty-file\\netty-client\\doc\\map-of-uae.geojson";

        //支持中文路径
       // gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        //设置为空时
        gdal.SetConfigOption("SHAPE_ENCODING", "");
        // Register the OGR library
        ogr.RegisterAll();



        // Open the DXF file
        DataSource dataSource = ogr.Open(inputDxfPath, 0);

        if (dataSource == null) {
            System.out.println("Error: Could not open DXF file.");
            System.exit(1);
        }

        // Create the GeoJSON driver
        org.gdal.ogr.Driver geoJsonDriver = ogr.GetDriverByName("GeoJSON");
        if (geoJsonDriver == null) {
            System.out.println("Error: GeoJSON driver not available.");
            System.exit(1);
        }

        // Create the GeoJSON output file
        DataSource outputDataSource = geoJsonDriver.CreateDataSource(outputGeoJsonPath);
        if (outputDataSource == null) {
            System.out.println("Error: Could not create GeoJSON file.");
            System.exit(1);
        }

        // Loop through each layer in the DXF file and copy it to the GeoJSON file
        int layerCount = dataSource.GetLayerCount();

        for (int i = 0; i < layerCount; i++) {
            org.gdal.ogr.Layer layer = dataSource.GetLayerByIndex(i);
            long l = layer.GetFeatureCount();
            for (int j = 0; j < l; j++) {
                System.out.println(layer.GetFeature(j).GetFieldAsString("Layer"));
            }
            String s = layer.GetName();
            outputDataSource.CopyLayer(layer, s);
        }

        // Close the datasources
        dataSource.delete();
        outputDataSource.delete();
    }
}
